package com.contory.ssn_lgnd.app.title;

import java.util.ArrayList;

import org.jbox2d.common.Vec2;

import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.KeyFrameAnimation;
import com.contory.ssn_lgnd.app.Settings;
import com.contory.ssn_lgnd.app.comp.ButtonObject;
import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.TouchPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class TitleStageSelectButtons implements UpdatePtr, DrawPtr, TouchPtr{
	ArrayList<TitleStageSelectBtrChd> btrList = new ArrayList<TitleStageSelectBtrChd>();
	ArrayList<Boolean> btrEndChklist = new ArrayList<Boolean>();
	int clickedId = -1;
	
	final static byte state_mov = 0;
	final static byte state_end = 1;
	final static byte state_clicked = 2;
	byte state = 0;
	
	public TitleStageSelectButtons(Title title){
		TitleStageSelectBtrChd chd = new TitleStageSelectBtrChd();
		chd.bool_locked = getLockFlag(0);
		chd.setXY_WH_TR(-2.5f, 7, 5, 1, Assets.tex_reg_button1);
		chd.setString("1");
		chd.setSound(Assets.sound_click);
		btrList.add(chd);
		
		chd = new TitleStageSelectBtrChd();
		chd.bool_locked = getLockFlag(1);
		chd.setXY_WH_TR(22.5f, 7, 5, 1, Assets.tex_reg_button1);
		chd.setString("2");
		chd.setSound(Assets.sound_click);
		btrList.add(chd);
		
		chd = new TitleStageSelectBtrChd();
		chd.bool_locked = getLockFlag(2);
		chd.setXY_WH_TR(-2.5f, 5, 5, 1, Assets.tex_reg_button1);
		chd.setString("3");
		chd.setSound(Assets.sound_click);
		btrList.add(chd);
		
		chd = new TitleStageSelectBtrChd();
		chd.bool_locked = getLockFlag(3);
		chd.setXY_WH_TR(22.5f, 5, 5, 1, Assets.tex_reg_button1);
		chd.setString("4");
		chd.setSound(Assets.sound_click);
		btrList.add(chd);
		
		chd = new TitleStageSelectBtrChd();
		chd.bool_locked = getLockFlag(4);
		chd.setXY_WH_TR(-2.5f, 3, 5, 1, Assets.tex_reg_button1);
		chd.setString("5");
		chd.setSound(Assets.sound_click);
		btrList.add(chd);
		
		chd = new TitleStageSelectBtrChd();
		chd.bool_locked = getLockFlag(5);
		chd.setXY_WH_TR(22.5f, 3, 5, 1, Assets.tex_reg_button1);
		chd.setString("6");
		chd.setSound(Assets.sound_click);
		btrList.add(chd);
		
		for(int i = 0; i < btrList.size(); i++){
			btrEndChklist.add(false);
		}
	}
	
	private boolean getLockFlag(int pos){
		if(Settings.stageFlagList[pos] == 0) // locked
			return true;
		else
			return false;
	}
	
	public int getStageNum(){
		return clickedId;
	}
	
	@Override
	public void setTouchEvent(TouchEvent touchEvent, float touchX, float touchY) {
		switch(state){
			case state_mov:
				break;
				
			case state_end:
				for (TitleStageSelectBtrChd chd : btrList) {
					chd.setTouchEvent(touchEvent, touchX, touchY);
				}
				break;
		}
	}

	@Override
	public void draw(SpriteBatcher batcher) {
		Assets.font.drawText(batcher, "SELECT STAGE", 10, 9, 1.2f, 1.2f, 1.0f, 1.0f, 1.0f, 1.0f);
		for (TitleStageSelectBtrChd chd : btrList) {
			chd.draw(batcher);
		}
	}

	@Override
	public void update(float deltaTime) {
		switch(state){
			case state_mov:
				for(int i = 0; i < btrList.size(); i++){
					if((i & 1) == 0){ //left side
						if(!btrEndChklist.get(i)){
							//btrList.get(i).velX += btrList.get(i).accX * deltaTime;
							if(KeyFrameAnimation.translateX(btrList.get(i), deltaTime, 12.0f, 6)){
								btrEndChklist.set(i, true);
								btrList.get(i).setBound();
							}
						}
					}
					else{
						if(!btrEndChklist.get(i)){
							//btrList.get(i).velX += btrList.get(i).accX * deltaTime;
							if(KeyFrameAnimation.translateX(btrList.get(i), deltaTime, -12.0f, 14)){
								btrEndChklist.set(i, true);
								btrList.get(i).setBound();
							}
						}
					}
				}
				boolean goNext = true;
				for(int i = 0; i < btrEndChklist.size(); i++){
					if(!btrEndChklist.get(i)){
						goNext = false;
						break;
					}
				}
				if(goNext)
					state = state_end;
			break;
			
			case state_end:
				for(int i = 0; i < btrList.size(); i++){
					btrList.get(i).update(deltaTime);
					if(btrList.get(i).excuteBool){
						clickedId = i;
						state = state_clicked;
						break;
					}
				}
			break;
			
			case state_clicked:
			break;
		}
	}
	
	public class TitleStageSelectBtrChd extends ButtonObject{
		public boolean bool_locked = true;
		//public float velX = 12.0f;
		//public float accX = 12.0f;
		
		@Override
		public void setTouchEvent(TouchEvent touchEvent, float touchX, float touchY){
			if(bool_locked)
				return;
			this.touchEvent = touchEvent;
			touchPoint.set(touchX, touchY);
		}
		
		@Override
		public void update(float deltaTime) {
			if(!bool_locked)
				super.update(deltaTime);
		}
		
		@Override
		public void draw(SpriteBatcher batcher) {
			super.draw(batcher);
			if(bool_locked)
				batcher.drawSprite(x, y, iconSize, iconSize, 1.0f, 1.0f, 1.0f, 1.0f, Assets.tex_reg_disable);
		}
		
		public void setBound(){
			bound.lowerLeft.set(x, y).sub(bound.width / 2, bound.height / 2);
	    }
	}
}
